/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.Die;
import Model.Player;
import Model.Token;
import View.BoardCreator;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import View.*;
import java.awt.Dimension;
import java.io.IOException;
import java.net.URL;
import javax.swing.*;

/**
 *
 * @author tg300
 */

public class BoardCreatorController {
    
    private BoardCreator boardView;
    private Die gameDie;
    private Player cmpPlayer;
    private Player humanPlayer;
    private Token compToken;
    private Token humanToken;
    private int cmpPlayerRoll;
    private int humanPlayerRoll;
    private int humanBoardPos = 1;
    private int compBoardPos = 1;
    private boolean start;
    String humanName;
    private URL helpURL;
    private static boolean DEBUG = false;
    private JEditorPane htmlPane;
    private int decider = 1;
    
    public BoardCreatorController (BoardCreator view, Die aDie, Player cmp, Player human, Token cmpToken, Token humToken) {

        boardView = view;
        gameDie = aDie;
        cmpPlayer = cmp;
        humanPlayer = human;
        compToken = cmpToken;
        humanToken = humToken;
        view.addBoardListener(new BoardCreatorListener());
        view.addMoveListener(new MoveListener());
        humanName = human.getName();
    }
    
    public class BoardCreatorListener extends JPanel implements ActionListener {
        
        public void actionPerformed(ActionEvent e) {

            if(e.getActionCommand().equals("ROLL!")) {

                if(decider == 1) {
                    start = decideGo();
                    if(start) {
                        boardView.setStartImage();
                    }
                    else {
                        boardView.setStartCPUImage();
                    }
                    decider++;
                    
                }          

                humanPlayerRoll = humanPlayer.roll(gameDie);
                boardView.setNumberRolled(humanPlayerRoll);
                
                cmpPlayerRoll = cmpPlayer.roll(gameDie);
                boardView.setCPUroll(cmpPlayerRoll);

                boardView.setMove(true);
                boardView.setRollButton(false);

            }//End if roll
             else if(e.getActionCommand().equals("HELP?"))
            {
                
                JFrame newFrame = new JFrame("New Window");
                
                 htmlPane = new JEditorPane();
                 htmlPane.setEditable(false);
                 System.out.println("About to call help method");
                 gameHelp();
                 JScrollPane htmlView = new JScrollPane(htmlPane);

                 newFrame.setSize(new Dimension(800, 600));
                 newFrame.setLocationRelativeTo(null);
                 newFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

                 newFrame.add(htmlView);
                 newFrame.setVisible(true);
            }//End if help
             
             else if(e.getActionCommand().equals("SAVE GAME")) 
             {


             }//End saving game
            
            else if (e.getActionCommand().equals("QUIT")) 
            {
            int answer = JOptionPane.showConfirmDialog(null, "Are you sure to quit game? \n Please be sure to press SAVE GAME \n if you wish to carry on later! ", "Warning", JOptionPane.YES_OPTION, JOptionPane.NO_OPTION);
            if (answer == JOptionPane.YES_OPTION) 
                {
                    JOptionPane.showMessageDialog(null, "Thank You for playing!");
                    System.exit(0);
                 }

            }//End if quit button

        }//End actionPerformed
    }//End button Action Listeners
    
    
    
    private void gameHelp(){
              String s = "/View/SEWebsite3.html";
              helpURL = getClass().getResource(s);
             System.out.println("This is the URL " + helpURL);
                 if (helpURL == null) {
                    System.err.println("Couldn't open help file: " + s);
                 } else if (DEBUG) {
                    System.out.println("Help URL is " + helpURL);
                 }

              displayURL(helpURL);

    }
     private void displayURL(URL url) {
            System.out.println("I'm inside display url method");

         try {
            if (url != null) {
                System.out.println("my url is not null");
                 htmlPane.setPage(url);
                 System.out.println("setting url pane");

            } else { //null url
              htmlPane.setText("File Not Found");
                if (DEBUG) {
                    System.out.println("Attempted to display a null URL.");
                }
            }
        } catch (IOException e) {
            System.err.println("Attempted to read a bad URL: " + url);
        }
    }//End display URL
    
    

    public class MoveListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            
            if(e.getActionCommand().equals(" ")) {

                if(start) {

                    boardView.setMove(false);
                    boardView.setRollButton(true);

                    humanBoardPos = humanGo("f");
                    boardView.setHumanToken(humanBoardPos);
                    boardView.setHumanText();
                    boolean didHumWin = boardView.checkWin(humanBoardPos);
                            if(didHumWin) {
                                ImagePanel img = new ImagePanel();
                                ImagePanelController control = new ImagePanelController(img);
                                boardView.killBoard();
                            }

                    compBoardPos = compGo("f");
                    boardView.setCompToken(compBoardPos);
                    boardView.setCompText();
                    
                    if(cmpPlayerRoll == 6 ) {
                        cmpPlayerRoll = cmpPlayer.roll(gameDie);
                        compBoardPos = compGo("f");
                        boardView.setCompToken(compBoardPos);
                        boardView.setCompText();
                        
                    }
                    
                    boolean didCompWin = boardView.checkCompWin(compBoardPos);
                            if(didCompWin) {
                                ImagePanel img = new ImagePanel();
                                ImagePanelController control = new ImagePanelController(img);
                                boardView.killBoard();
                            }

                    
                }
                else {

                    boardView.setMove(false);
                    boardView.setRollButton(true);

                    compBoardPos = compGo("f");
                    boardView.setCompToken(compBoardPos);
                    boardView.setCompText();
                    
                    if(cmpPlayerRoll == 6 ) {
                        cmpPlayerRoll = cmpPlayer.roll(gameDie);
                        compBoardPos = compGo("f");
                        boardView.setCompToken(compBoardPos);
                        boardView.setCompText();
                        
                    }
                     
                    boolean didCompWin = boardView.checkCompWin(compBoardPos);
                            if(didCompWin) {
                                ImagePanel img = new ImagePanel();
                                ImagePanelController control = new ImagePanelController(img);
                                boardView.killBoard();
                            }

                    humanBoardPos = humanGo("f");
                    boardView.setHumanToken(humanBoardPos);
                    boardView.setHumanText();
                    boolean didHumWin = boardView.checkWin(humanBoardPos);
                            if(didHumWin) {
                                ImagePanel img = new ImagePanel();
                                ImagePanelController control = new ImagePanelController(img);
                                boardView.killBoard();
                            }

                    
                }

            }
            else if(e.getActionCommand().equals("  ")) {

                if(start) {

                    boardView.setMove(false);
                    boardView.setRollButton(true);

                    humanBoardPos = humanGo("b");
                    boardView.setHumanToken(humanBoardPos);
                    boardView.setHumanText();
                    boolean didHumWin = boardView.checkWin(humanBoardPos);
                            if(didHumWin) {
                                ImagePanel img = new ImagePanel();
                                ImagePanelController control = new ImagePanelController(img);
                                boardView.killBoard();
                            }

                    compBoardPos = compGo("f");
                    boardView.setCompToken(compBoardPos);
                    boardView.setCompText();
                    
                    if(cmpPlayerRoll == 6 ) {
                        cmpPlayerRoll = cmpPlayer.roll(gameDie);
                        compBoardPos = compGo("f");
                        boardView.setCompToken(compBoardPos);
                        boardView.setCompText();
                        
                    }
                    
                    boolean didCompWin = boardView.checkCompWin(compBoardPos);
                            if(didCompWin) {
                                ImagePanel img = new ImagePanel();
                                ImagePanelController control = new ImagePanelController(img);
                                boardView.killBoard();
                            }

                }
                else {

                    boardView.setMove(false);
                    boardView.setRollButton(true);

                    compBoardPos = compGo("f");
                    boardView.setCompToken(compBoardPos);
                    boardView.setCompText();
                    
                    if(cmpPlayerRoll == 6 ) {
                        cmpPlayerRoll = cmpPlayer.roll(gameDie);
                        compBoardPos = compGo("f");
                        boardView.setCompToken(compBoardPos);
                        boardView.setCompText();
                        
                    }
                    
                    boolean didCompWin = boardView.checkCompWin(compBoardPos);
                            if(didCompWin) {
                                ImagePanel img = new ImagePanel();
                                ImagePanelController control = new ImagePanelController(img);
                            }

                    humanBoardPos = humanGo("b");
                    boardView.setHumanToken(humanBoardPos);
                    boardView.setHumanText();
                    boolean didHumWin = boardView.checkWin(humanBoardPos);
                            if(didHumWin) {
                                ImagePanel img = new ImagePanel();
                                ImagePanelController control = new ImagePanelController(img);
                            }

                   
                }
            }
        }
    }
    
    public boolean decideGo() {
        
            JOptionPane.showMessageDialog(null, "Lets see who will go first, " + humanName + " or CPU?");
            //Inform user of following process
            cmpPlayerRoll = cmpPlayer.roll(gameDie);
            JOptionPane.showMessageDialog(null, "So CPU rolls first..." + "\n And CPU rolled a: " + cmpPlayerRoll + "!");
            //Finish computers first roll
            humanPlayerRoll = humanPlayer.roll(gameDie);
            JOptionPane.showMessageDialog(null, "Now lets see what you roll..." + "\n" + humanName +" roled a: " + humanPlayerRoll);
            //Finish humans first roll
            
           
            if (humanPlayerRoll > cmpPlayerRoll) 
            {
                JOptionPane.showMessageDialog(null, "CONGRATULATIONS!" + "\n As " + humanPlayerRoll +" is higher than " + cmpPlayerRoll + ", You go first!");
                humanPlayer.setTurn(true);
                cmpPlayer.setTurn(false);
                return true;
            } 
            else if (humanPlayerRoll == cmpPlayerRoll) 
            {
                JOptionPane.showMessageDialog(null, "Tie! Press roll again");
                return decideGo();
            } 
            else 
            {
                JOptionPane.showMessageDialog(null,"Oh dear, looks like CPU will go first then!");
                humanPlayer.setTurn(false);
                cmpPlayer.setTurn(true);
            }
            
            return false;
    }//End DecideGo
    
    public int humanGo(String move) {
            int currentPosition = humanToken.getPosition().getNumber();
            //int humanRolled = humanPlayer.roll(gameDie);

            boolean canMove = checkMove(humanPlayerRoll, humanToken, humanPlayer);

            if (canMove) {

                    if (move.equals("f")) {
                        
                        humanToken.moveCmpToken(humanPlayerRoll, move);
                    } else if (move.equals("b")) {
                        
                        boolean canMoveBack = MoveBack(humanPlayerRoll, humanToken);
                        if (canMoveBack) {

                            humanToken.moveCmpToken(humanPlayerRoll, move);
                        } else {

                            JOptionPane.showMessageDialog(null, "Backwards not possible!");
                            humanToken.moveCmpToken(humanPlayerRoll, "f");
                        }
                    }

                boolean bumper = checkBump(humanToken, compToken);

                if (bumper) {
                   // JOptionPane.showMessageDialog(null, "The CPU just got bumped!");
                    boardView.setCompToken(compToken.getPosition().getNumber());
                    boardView.setCompText();
                    boardView.setHumanScore(5);
                }


                currentPosition = humanToken.getPosition().getNumber();
            }

        return currentPosition;
   }
    
    public int compGo(String move) {
        int currentPosition = compToken.getPosition().getNumber();
        //int computerRolled = cmpPlayer.roll(gameDie);
        boolean canMove = checkMove(cmpPlayerRoll, compToken, cmpPlayer);
        if (canMove) {
            
            compToken.moveCmpToken2(cmpPlayerRoll, humanToken.getPosition().getNumber());
            boolean bumper = checkBump(compToken, humanToken);

                if(bumper) {
                        System.out.println("human bumped");
                        boardView.setHumanToken(humanToken.getPosition().getNumber());
                        boardView.setHumanText();
                        boardView.setCompScore(5);
                }

            currentPosition = compToken.getPosition().getNumber();
        }
        return currentPosition;
    }
    
   public boolean checkMove(int roll, Token t, Player p) {
       
        int totalSquares = boardView.getTotal();
        int possibleMove = roll + t.getPosition().getNumber();

        if (possibleMove <= totalSquares) {
            return true;
        } 
        

        return false;

    }

    public boolean checkBump(Token t1, Token t2) {
        if (t1.getPosition().getNumber() == t2.getPosition().getNumber()) {
            t2.bump();
            return true;
        }
        return false;
    }
    
    public boolean MoveBack(int roll, Token t) {
        int possibleMove;
        possibleMove = t.getPosition().getNumber() - roll;
       

        if (possibleMove >= 1) {
            return true;
        } else {
            return false;
        }
    }
}
